import Vue from 'vue'
import axios from 'axios'
import store from '../store'
var qs = require('qs')
// 响应时间
axios.defaults.timeout = 5 * 1000
// 配置cookie
// axios.defaults.withCredentials = true

// 静态资源
Vue.prototype.$static = ''

// 配置接口地址
axios.defaults.baseURL = 'http://yqsc.cqfuyuan.cn';
axios.defaults.headers['Content-Type'] = 'application/json';
// var loadingInstance
// POST传参序列化(添加请求拦截器)
axios.interceptors.request.use(
    config => {
        // loadingInstance = Loading.service({
        //     lock: true,
        //     text: '数据加载中，请稍后...',
        //     spinner: 'el-icon-loading',
        //     background: 'rgba(1, 1, 1, 0.7)'
        // })
        return config
    },
    err => {
        console.log("失败")
        // loadingInstance.close()
        // Notification.error({
        //     title: '请求失败',
        //     message: err.response.data.message
        // });
        return Promise.reject(err)
    }
)

// 返回状态判断(添加响应拦截器)
axios.interceptors.response.use(
    res => {
        if (res.data.code === 200) {
            //     loadingInstance.close()
            return res
        } else {
            //     loadingInstance.close()
            Vue.$vux.toast.show({
                text: res.data.message,
                position: 'middle',
                type: 'text'
            })
        }
    }, err => {
        // loadingInstance.close()
        // Notification.error({
        //     title: '请求失败',
        //     message: err.response.data.message
        // });
        return Promise.reject(err)
    }
)
// 发送请求
export function fetchPost(url, params) {
    return new Promise((resolve, reject) => {
        axios.defaults.headers.common['Authorization'] = store.state.user.token;
        axios.post(url, params).then(
            res => {
                resolve(res.data)
            },
            err => {
                reject(err.data)
            }
        ).catch(err => {

            reject(err.data)
        })
    })
}

export function fetchGet(url, params) {
    return new Promise((resolve, reject) => {
        axios.defaults.headers.common['Authorization'] = store.state.user.token;
        axios.get(url, params).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err.data)
        })
    })
}

